#include "ctaskproc.h"

CtaskProc::CtaskProc(QTcpSocket* &Client, QString Ip, quint16 Port, QObject *parent) :QObject(parent), ForwardClient(Client), ForwardIp(Ip), ForwardPort(Port)
{
    QObject::connect(ForwardClient, SIGNAL(readyRead()), this, SLOT(RecvClientData()));
    QObject::connect(ForwardClient, SIGNAL(disconnected()), this, SLOT(Diconnect()));

    ForwardServer = new QTcpSocket();
    QObject::connect(ForwardServer, SIGNAL(readyRead()), this, SLOT(RecvSerData()));
    QObject::connect(ForwardServer, SIGNAL(connected()), this, SLOT(Connected()));
    QObject::connect(ForwardServer, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(ReadError(QAbstractSocket::SocketError)));
    QObject::connect(ForwardServer, SIGNAL(disconnected()), this, SLOT(Diconnect()));
}

CtaskProc::~CtaskProc()
{
    ForwardClient->close();
    ForwardServer->close();
}

void CtaskProc::StartForwardProc()
{
    ForwardServer->connectToHost(QHostAddress(ForwardIp), ForwardPort);
}

void CtaskProc::Connected()
{

}

void CtaskProc::ReadError(QAbstractSocket::SocketError)
{
    ForwardClient->close();
    ForwardServer->close();
}

void CtaskProc::Diconnect()
{
    ForwardClient->close();
    ForwardServer->close();
}

void CtaskProc::RecvClientData()
{
    QByteArray Byte = ForwardClient->readAll();
    ForwardServer->write(Byte);
}

void CtaskProc::RecvSerData()
{
    QByteArray Byte = ForwardServer->readAll();
    ForwardClient->write(Byte);
}
